package com.starsoft.frame.base.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.base.bean.BaseAttachment;
import com.starsoft.frame.base.service.AttachmentService;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.frame.util.StringUtil;

@RestController
@RequestMapping(value = "/base/attachment")
public class AttachementController {
	
	@Autowired
	private AttachmentService attachmentService;
	
	@RequestMapping(value = "/download")
	public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id = Integer.parseInt(request.getParameter("id"));
		BaseAttachment file = attachmentService.getFile(id);
		String filedisplay = new String(file.getFileName().getBytes(),"iso8859-1");
		response.setContentType("application/x-download");
		response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);	
		attachmentService.write(response.getOutputStream(), file.getContent());
	}
	
	@RequestMapping(value = "/delete")
	public Map<String, Object> delete(HttpServletRequest request) throws IOException {
		String idStr = request.getParameter("id");
		if(StringUtil.isEmpty(idStr)){
			return null;
		}
		Integer id = Integer.parseInt(idStr);
		attachmentService.delete(id);
		return WebUtil.sucesseModel("成功删除附件");
	}
	
}